package com.taobao.askwho.utils.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UrlCatcher {
	public static void readUrlFromFile(String sourceFile, final Map<String,String> urlMap) {
		StringBuilder sb = new StringBuilder();
		try {
	        //BufferedWriter writer  = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\tmp\\monitor.log_f"),"gbk"));
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(sourceFile)), "utf-8"));				
			String line = null;
			while((line=reader.readLine())!=null){
				sb.append(line.toLowerCase());
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String regstr="((?<=[\\s+]?href[\\s+]?=[\\s+]?('|\")?)[^\"|'>]+?(?=\"|'))(.+?)?((?<=>)(.+?)?(?=</a>))";
		Pattern   p   =   Pattern.compile(regstr);                 
		Matcher   m   =   p.matcher(sb.toString()); 
		//tag, url
		while(m.find()){ 
			String url = m.group(1).trim();
			String tagname = m.group(4).trim();
			tagname = tagname.replaceAll("《", "").replace("》", "").replace("[", "").replace("]", "").replaceAll("&quot;", "\"");
			if((!url.startsWith("http:") && !url.startsWith("https:")) || tagname.startsWith("<img") || tagname.startsWith("<link"))
				continue;
			if(tagname.startsWith("<em")) {
				tagname = tagname.substring(tagname.indexOf('>')+1,tagname.lastIndexOf("</em>"));
			}
			urlMap.put(tagname, url);
			//System.out.println(tagname + "=== " + url);			
		} 
		//System.out.println( "该次查找获得匹配组的数量为： "+m.groupCount()); 		
	}
	
	public static void displayMap(Map<String, String> map) {
		if(map != null) {
			for(Entry<String, String> entry : map.entrySet()) {
				System.out.println(entry.getKey() + "====" + entry.getValue());
			}
			System.out.println("map size = " + map.size());
		} else {
			System.out.println("map is null");
		}
	}
	public static void main(String[] args) throws FileNotFoundException {
		Map<String, String> map = new HashMap<String, String>();
		String[] fileArray = new String[]{
				"C:\\Users\\zhongting.zy\\Desktop\\网址\\阿里味儿 - Alibaba Group.htm",
				"C:\\Users\\zhongting.zy\\Desktop\\网址\\阿里味儿 - Alibaba Group2.htm",
				"C:\\Users\\zhongting.zy\\Desktop\\网址\\淘宝之家.txt",
				"C:\\Users\\zhongting.zy\\Desktop\\网址\\淘宝之家内网常用url.txt",
				"C:\\Users\\zhongting.zy\\Desktop\\网址\\淘宝直接域名老版本.txt",
				"C:\\Users\\zhongting.zy\\Desktop\\网址\\内网导航全部版.htm"
		};
		for(String fileName: fileArray) {
			UrlCatcher.readUrlFromFile(fileName, map);
		}
		System.out.println("**************************");//254 个网址
		UrlCatcher.displayMap(map);
	}			

}